#include <iostream>
#include <SDL_net.h>
#include "Networking.h"
#include "Server.h"
#include "Timer.h"
#include "Client.h"
#include "GameMessageFactory.h"

#undef main
int main(int argc, char** argv)
{
#pragma region Memory Leaks
#ifdef WIN32
    // Enable run-time memory check for debug builds.
#if defined(DEBUG) | defined(_DEBUG)
    _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif//debug
#endif//win32
#pragma endregion

    SDL_Init(0);
    SDLNet_Init();

    GameMessage::StaticPrepareFactory();
    TheTimer::Instance();
    TheServer::Instance();
    TheClient::Instance();

    if(argc < 2 )
    {
        OUT_STRING("Usage: " << " @param1: Socket to listen on ");
        return 1;
    }
    system("ipconfig");
    OUT_STRING('\n'<< '\n' << "----Starting----");

    int port = 0;
    OUT_STRING("Port: " << argv[1]);
    port = atoi(argv[1]); 


    TheServer::Instance()->StartUp(port);
    bool running = true;
    int counter = 0;
    while (running)
    {
        TheTimer::Instance()->Update();
        TheServer::Instance()->Update();
        SDL_Delay(10);
        counter++;
        if(counter > 10000)
            running = false;
    }

    TheServer::Instance()->ShutDown();  
    TheTimer::Kill();
    TheServer::Kill();
    TheClient::Kill();
    TheGameMessageFactory::Kill();
    system("pause");
} 